<template>
	<view class="retail">
		<view class="from_user_info">
			<uni-forms :modelValue="baseFormData.userInfo">
				<uni-forms-item label="患者姓名:">
					<uni-easyinput class="uni-mt-5" trim="all" v-model="baseFormData.userInfo.name"
						placeholder="请输入患者姓名"></uni-easyinput>
					<view class="more" @click="moreClick">
						{{baseFormData.more}}
					</view>
				</uni-forms-item>
				<uni-forms-item label="联系方式:">
					<uni-easyinput v-model="baseFormData.userInfo.tel" placeholder="请输入联系方式" />
				</uni-forms-item>
				<uni-forms-item label="性别:" v-if="baseFormData.more == '收缩'">
					<uni-data-checkbox v-model="baseFormData.userInfo.gender" :localdata="baseFormData.sexs" />
				</uni-forms-item>
				<uni-forms-item label="出生日期:" v-if="baseFormData.more == '收缩'">
					<picker placeholder="123" mode="date" :value="baseFormData.userInfo.birthday"
						@change="bindDateChange">
						<view class="uni-input">{{baseFormData.userInfo.birthday}} ></view>
					</picker>
				</uni-forms-item>
				<uni-forms-item label="年龄:" v-if="baseFormData.more == '收缩'">
					{{baseFormData.userInfo.age}}
				</uni-forms-item>
				<uni-forms-item label="身份证号:" v-if="baseFormData.more == '收缩'">
					<uni-easyinput v-model="baseFormData.userInfo.certno" placeholder="请输入身份证号" />
				</uni-forms-item>
			</uni-forms>
			<view class="from_item">

			</view>
		</view>
		<view class="Template_btn">
			<view class="Save" @click="SaveClick">
				存模板
			</view>
			<view class="apply" @click="applyClick">
				模板
			</view>
		</view>
		<view class="add_drug">
			<view class="drug_title">
				添加药品
			</view>
			<view class="search_drug">
				<uni-easyinput @confirm='drugSearch' prefixIcon="search" v-model="data.drugValue"
					placeholder="请输入药品名称/拼音简拼" />
				<img @click="onClickScan" src="@/static/img/sm.png" alt="" />
			</view>
			<view class="search" @click.self="closeDrug" v-if="baseFormData.searchList.length > 0">
				<view class="search_result">
					<view class="search_item" @click.stop="addDrug(item)" v-for="item in baseFormData.searchList"
						:key="item.id">
						<view class="drug_text">
							<view class="drug_text_title">
								<text class="name">{{item.name}}</text>
								<text class="lable">零售价：</text>
								<text class="money">￥{{item.pric}}</text>
							</view>
							<view class="drug_text_desc">
								<text>{{item.specification}}</text>
								<text>{{item.manufacturer}}</text>
								<text>库存: {{item.inventoryMin}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="add_project">
			<view class="project_title">
				添加项目
			</view>
			<view class="search_project">
				<uni-easyinput @confirm='therapySearch' prefixIcon="search" v-model="data.projectValue"
					placeholder="请输入项目名称/拼音简码" />
			</view>
			<view class="search" @click.self="closeDrug" v-if="baseFormData.therapyList.length > 0">
				<view class="search_result">
					<view class="search_item" @click.stop="addDrug(item)" v-for="item in baseFormData.therapyList"
						:key="item.id">
						<view class="drug_text">
							<view class="drug_text_title">
								<text class="name">{{item.name}}</text>
							</view>
							<view class="drug_text_desc">
								<text>{{item.amt}}</text>/
								<text>{{item.unit}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="view_ul">
				<view class="view_li" v-for="(item,index) in baseFormData.drugList" :key="item.id">
					<view class="li_title">
						<view class="name">
							{{item.name}}
						</view>
						<view class="money">
							<text>单价:</text>
							<uni-easyinput clearSize="12" class="uni-mt-5" trim="all" v-model="item.pric"
								placeholder="请输入内容"></uni-easyinput>
							<text>元</text>
						</view>
						<view class="number">
							<uni-number-box :min="1" :value="item.sinDoscnt" />
							<text>{{item.sinDosunt}}</text>
						</view>
						<view class="detele">
							<img @click="drugChange(item)" src="@/static/img/delete.png" alt="" />
						</view>
					</view>
					<view class="li_desc">
						<text>{{item.specification}}</text>
						<text>{{item.manufacturer}}</text>
					</view>
					<view class="li_params">
						<view class="params">
							<text>单次用量</text>
							<uni-easyinput clearSize="12" class="uni-mt-5" trim="all" v-model="item.sinDoscnt"
								placeholder="请输入内容"></uni-easyinput>
							<text class="value" @click="popup1Fn(1,index)">{{item.sinDosunt || '选择单位'}}</text>
						</view>
						<view class="use">
							<text>频率</text>
							<text class="value" @click="popup1Fn(2,index)">{{item.rxFrquName || '选择频率'}}</text>
						</view>
						<label v-if="false">
							<checkbox value="c" disabled="false" />拆零
						</label>
					</view>
				</view>
			</view>
		</view>
		<footer class="footers">
			<view class="money">
				￥{{AllMoney}}
			</view>
			<view class="operation">
				<view class="registration" @click="placeAnOrder('挂单')">
					挂单
				</view>
				<view class="charge" @click="placeAnOrder('收费')">
					收费
				</view>
			</view>
		</footer>
		<uni-popup ref="popup" class="withdrawal" :mask-click="false">
			<view class="popup-content">
				<div class="popup_title">
					零售列表
					<img @click="popup.close()" src="@/static/img/close.png" alt="" />
				</div>

				<view class="drug_box" v-for="item in baseFormData.temelapeList" :key="item.id">
					<text>{{item.name}}</text>
					<view class="popup_btn">
						<view class="close" @click="viewClick(item)">
							查看
						</view>
						<view class="enter" @click="useClick(item)">
							使用
						</view>
					</view>
				</view>

			</view>
		</uni-popup>
		<uni-popup ref="popupitem" class="withdrawal1" :mask-click="false">
			<view class="popup-content">
				<div class="popup_title">
					零售模板详情
					<img @click="popupitem.close()" src="@/static/img/close.png" alt="" />
				</div>
				<view class="popupitem">
					<ul class="popupitem_ul">
						<li class="popupitem_li">
							<text>模板名称:</text>
							<text>{{data.temelapeInfo.name}}</text>
						</li>
						<li class="popupitem_li">
							<text>说明:</text>
							<text>{{data.temelapeInfo.remark}}</text>
						</li>
						<li class="popupitem_li">
							<text>创建人:</text>
							<text>{{data.temelapeInfo.createUser}}</text>
						</li>
					</ul>
					<ul class="drug_ul" v-for="y in baseFormData.templateDetailList" :key="y.id">
						<li class="drug_title">{{y.name}}</li>
						<li class="drug_li" v-for="item in y.details" :key="item.id">
							<view class="top">
								<view class="text_title">
									{{item.name}}
								</view>
								<view class="param">
									<view>零售价:<text>{{item.pric}}{{item.unit}}</text></view>
									<view>数量:<text>{{item.cnt}}</text></view>
								</view>
							</view>
							<view class="bottom">
								<view>金额:<text>{{item.amt}}</text></view>
								<view>单次剂量:<text>{{item.rxDoscnt}}{{item.sinDosunt}}</text></view>
								<view>频率:<text>{{item.rxFrquName}}</text></view>
								<view v-if="false">拆零购买:<text>否</text></view>
							</view>
						</li>
					</ul>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="popup1" class="withdrawal2" :mask-click="false">
			<view class="popup-content">
				<div class="popup_title">
					添加模板
					<img @click="popup1.close()" src="@/static/img/close.png" alt="" />
				</div>
				<view class="add_temelate">
					<uni-forms ref="baseForm" :modelValue="data.template">
						<uni-forms-item label="模板名称" required>
							<uni-easyinput v-model="data.template.Name" placeholder="请输入模板名称" />
						</uni-forms-item>
						<uni-forms-item label="模板类型" required>
							<uni-easyinput v-model="data.template.PatternName" placeholder="请输入年龄" />
						</uni-forms-item>
						<uni-forms-item label="模板声明">
							<uni-easyinput v-model="data.template.Remark" placeholder="请输入说明" />
						</uni-forms-item>
						<uni-forms-item label="sex" class="sex">
							<uni-data-checkbox v-model="data.template.isPersonalTemplate" :localdata="data.sexs" />
						</uni-forms-item>
					</uni-forms>
				</view>
				<view class="popup_btn">
					<view class="close" @click="popup1.close()">
						取消
					</view>
					<view class="enter" @click="enterAddTemplate">
						确定
					</view>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="popup_pay" class="pay" :mask-click="false">
			<view class="popup-content">
				<div class="popup_title">
					收费
				</div>
				<view class="repair">
					<ul class="repair_ul">
						<li class="repair_title">
							收费信息
						</li>
						<li class="repair_li">
							<text>总金额:</text>￥{{AllMoney}}
						</li>
						<li class="repair_li">
							<text>优惠方式:</text>
							优惠金额<uni-easyinput class="uni-mt-5" @blur="value1Change" v-model="fromData.value1"
								trim="all" placeholder="0.00"></uni-easyinput>元
							<text class="text_margin">折扣</text><uni-easyinput class="uni-mt-5" trim="all"
								@blur="value2Change" v-model="fromData.value2" placeholder="0"></uni-easyinput>
						</li>
						<li class="repair_li">
							应收金额:<text class="red">￥{{money}}</text>
						</li>
						<li class="repair_li">
							<text>欠费金额:</text><uni-easyinput v-model="fromData.value3"
								placeholder="0.00"></uni-easyinput>元
							<uni-data-checkbox multiple @change="value4Change" v-model="fromData.value4"
								:localdata="data.hobby">全部欠费</uni-data-checkbox>
						</li>
						<li class="repair_li">
							<text>备注:</text><uni-easyinput v-model="fromData.value5"
								placeholder="请输入备注"></uni-easyinput>
						</li>
					</ul>
				</view>
				<view class="RefundMethod">
					<view class="Method_title">
						支付方式
					</view>
					<ul class="pay_ul">
						<li :class="['pay_li',{'active':data.payId == item.id}]" v-for="item in data.payList"
							:key="item.id" @click="payFnClick(item)">
							{{item.name}}
						</li>
					</ul>
				</view>
				<view class="popup_btn">
					<view class="close" @click="popup_pay.close()">
						取消
					</view>
					<view class="enter" @click="enterPay()">
						确定
					</view>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="popup_user" class="add_user_Info" :mask-click="false">
			<view class="popup-content">
				<div class="popup_title">
					客户信息登记
				</div>
				<uni-forms ref="baseForm" :modelValue="userFormData">
					<uni-forms-item label="姓名:" required>
						<uni-easyinput v-model="userFormData.name" placeholder="请输入姓名" />
					</uni-forms-item>
					<uni-forms-item label="性别:" required>
						<uni-data-checkbox v-model="userFormData.sex" :localdata="baseFormData.sexs" />
					</uni-forms-item>
					<uni-forms-item label="年龄:" required>
						<uni-easyinput v-model="userFormData.age" placeholder="请输入年龄" />
					</uni-forms-item>
					<uni-forms-item label="联系电话:" required>
						<uni-easyinput v-model="userFormData.tel" placeholder="请输入联系电话" />
					</uni-forms-item>
					<uni-forms-item label="身份证:" required>
						<uni-easyinput v-model="userFormData.card" placeholder="请输入身份证" />
					</uni-forms-item>
					<uni-forms-item label="联系地址:">
						<uni-easyinput v-model="userFormData.address" placeholder="请输入联系地址" />
					</uni-forms-item>
					<uni-forms-item label="备注:">
						<uni-easyinput v-model="userFormData.remark" placeholder="请输入备注" />
					</uni-forms-item>
				</uni-forms>

				<view class="popup_btn">
					<view class="close" @click="popup_user.close()">
						取消
					</view>
					<view class="enter" @click="userInfoenter()">
						确定
					</view>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="popup_type" class="withdrawal3" :mask-click="false">
			<view class="popup-content">
				<picker-view indicator-style="height: 50px;" @change="bindChange" class="picker-view">
					<picker-view-column>
						<view class="item" v-for="item in data.selectList" :key="item.value">{{item.text}}</view>
					</picker-view-column>
				</picker-view>
				<view class="popup_btn">
					<view class="close" @click="popup_type.close()">
						取消
					</view>
					<view class="enter" @click="AddTemplateType">
						确定
					</view>
				</view>
			</view>
		</uni-popup>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="info" cancelText="继续零售" confirmText="返回首页" title="提示" content="挂单成功!"
				@confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script setup>
	import {
		ref,
		reactive,
		onMounted,
		computed
	} from "vue"
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		getDetailApi,
	} from "@/api/retailrecords/index.js"
	import {
		SetAddTtemplateApi,
		getTemplateListApi,
		getusedFrquSelector,
		getDeatilsApi,
		setplaceAnOrderApi,
		getunitFrquSelector,
		getdrugSelector,
		gettherapySelector,
		settlement
	} from "@/api/retail/index.js"
	onLoad((option) => {
		data.prescriptionId = option.id
	})
	let popup = ref()
	let popupitem = ref()
	let popup1 = ref()
	let popup_pay = ref()
	let popup_user = ref()
	let popup_type = ref()
	const alertDialog = ref()
	const data = reactive({
		prescriptionId: '',
		popupType: 0,
		popupIndex: null,
		value: {
			PatternName: '',
			pattern: '',
		},
		drugValue: '',
		projectValue: '',
		temelapeInfo: {},
		settlement: {},
		sexs: [{
			text: '个人专用',
			value: 0
		}, {
			text: '诊所共享',
			value: 1
		}],
		template: {
			Name: '',
			PatternName: '零售模板',
			pattern: '9', // 9
			Remark: '',
			isPersonalTemplate: 1,
		},
		payList: [{
				name: '现金',
				id: 1,
			},
			{
				name: '支付宝',
				id: 2,
			},
			{
				name: '微信',
				id: 3,
			},
			{
				name: '银行卡',
				id: 4,
			},
			{
				name: '商保',
				id: 5,
			},
			{
				name: '其他',
				id: 6,
			}
		],
		hobby: [{
			text: '全部欠费',
			value: 0
		}],
		selectList: []
	})
	const money = ref(0)
	const fromData = reactive({
		value1: '',
		value2: '',
		value3: '',
		value4: [],
		value5: '',
	})
	const userFormData = reactive({
		name: '',
		sex: '男',
		age: '',
		tel: '',
		card: '',
		address: '',
		remark: '',
	})
	const baseFormData = reactive({
		userInfo: {
			name: '',
			tel: '',
			gender: '',
			birthday: '请选择出生日期',
			age: '',
			certno: '',
		},
		sexs: [{
			text: '男',
			value: '1'
		}, {
			text: '女',
			value: '2'
		}],
		searchList: [], // 药品搜索列表
		therapyList: [], // 药品搜索列表
		param: [], // 用量单位
		frqusekect: [], // 频率
		more: '编辑更多',
		drugList: [], // 药品列表
		templateDetailList: [], // 模板详情药品列表
		temelapeList: [], // 模板列表
	})
	let AllMoney = computed(() => {
		let num = 0
		baseFormData.drugList.forEach(item => {
			num += (item.pric * item.sinDoscnt)
		})
		money.value = num
		return (num+'') == 'NaN' ? 0 : num
	})
	const bindDateChange = (e) => {
		baseFormData.userInfo.birthday = e.detail.value
	}
	const popup1Fn = (type, index) => {
		data.popupType = type
		data.popupIndex = index
		if (type == 1) {
			data.selectList = baseFormData.param
		} else if (type == 2) {
			data.selectList = baseFormData.frqusekect
		}
		data.value.PatternName = data.selectList[0].text
		data.value.pattern = data.selectList[0].value
		popup_type.value.open('bottom')
	}
	const bindChange = (e) => {
		const value = e.detail.value[0]
		console.log(value);
		data.selectList.forEach((item, index) => {
			if (index == value) {
				data.value.PatternName = item.text
				data.value.pattern = item.value
			}
		})
	}
	const AddTemplateType = () => {
		if (data.popupType == 1) {
			baseFormData.drugList.forEach((item, index) => {
				if (index == data.popupIndex) {
					item.sinDosunt = data.value.PatternName
					console.log(item.sinDosunt, data.value.PatternName);
				}
			})
		} else if (data.popupType == 2) {
			baseFormData.drugList.forEach((item, index) => {
				if (index == data.popupIndex) {
					item.rxFrquName = data.value.PatternName
				}
			})
		}

		popup_type.value.close()
	}
	const dialogConfirm = () => {
		uni.switchTab({
			url: '/pages/index/index'
		});
	}
	const dialogClose = () => {
		uni.redirectTo({
			url: '/pages/retail/index'
		})
	}
	// 挂单--收费
	const placeAnOrder = async (type) => {
		if (baseFormData.drugList.length == 0) {
			uni.showToast({
				title: '请选择药品或者项目',
				duration: 2000,
				icon: 'error'
			})
			return
		}
		if (!baseFormData.userInfo.name) {
			uni.showToast({
				title: '请填写患者名称',
				duration: 2000,
				icon: 'error'
			})
			return
		}
		if (!baseFormData.userInfo.birthday) {
			uni.showToast({
				title: '请填写患者出生日期',
				duration: 2000,
				icon: 'error'
			})
			return
		}
		if (!baseFormData.userInfo.tel) {
			uni.showToast({
				title: '请填写患者联系方式',
				duration: 2000,
				icon: 'error'
			})
			return
		}

		baseFormData.userInfo = {
			"orgId": 252885263003720,
			"name": "大理石",
			"birthday": "1995-12-03 00:00:00",
			"age": 28.0,
			"tel": "13888888888",
			"fixPhone": null,
			"eMail": null,
			"certno": null,
			"gender": "1",
			"maritalStatus": false,
			"naty": null,
			"faith": null,
			"patientSource": null,
			"referrer": null,
			"referrals": null,
			"hobbies": null,
			"remark": null,
			"occupation": null,
			"province": "",
			"city": "",
			"area": "",
			"address": null,
			"pinYin": "dls",
			"tagIds": null,
			"empr": null,
			"monthlyProfit": 0.0,
			"allergy": null,
			"previousHistory": null,
			"isGravida": false,
			"isLactation": false,
			"isDelete": false,
			"createTime": "2023-12-03 16:44:42",
			"updateTime": "2023-12-06 21:29:58",
			"createUserId": 212725263002001,
			"updateUserId": 212725263002001,
			"createUser": "superAdmin",
			"updateUser": "superAdmin",
			"id": 489176595996741,
			"extJson": null
		}
		baseFormData.drugList = [{
			"prescriptionId": 490308673564742,
			"drugId": 488886629195845,
			"examinationTherapyId": 0,
			"extraChargeId": 0,
			"name": "逍遥丸",
			"dosageForm": null,
			"enableSplit": false,
			"splitRatio": null,
			"splitPrice": null,
			"unit": "盒",
			"splitUnit": null,
			"sinDosunt": null,
			"rxUsedWayName": null,
			"days": 1,
			"pric": 18.0,
			"cnt": 1,
			"derate": 0.0,
			"returnableQuantity": 0.0,
			"returnDate": null,
			"sinDoscnt": "1",
			"rxDoscnt": 1,
			"amt": 18.0,
			"rxFrquName": null,
			"groupId": null,
			"skinTest": false,
			"acupoint": null,
			"specialTreatment": null,
			"doctorAdvice": null,
			"surchargeName": null,
			"itemType": 1,
			"itemTypeStr": "西药中成药",
			"isDispensing": false,
			"medListCodg": null,
			"medListName": null,
			"manufacturer": "九芝堂股份有限公司",
			"specification": "0.2g*200粒",
			"cntCount": 1,
			"isDelete": false,
			"createTime": null,
			"updateTime": null,
			"createUserId": null,
			"updateUserId": null,
			"createUser": null,
			"updateUser": null,
			"id": 0,
			"extJson": null
		}]
		let res = await setplaceAnOrderApi({
			patient: baseFormData.userInfo,
			prescriptionDetails: baseFormData.drugList
		})
		if (res.code == 200) {
			if (type == '挂单') {
				alertDialog.value.open()
			} else {
				data.settlement = res.data
				popup_pay.value.open('bottom')
			}
		} else {
			uni.showToast({
				duration: 2000,
				title: res.msg,
				icon: "error"
			})
		}
	}
	// 频率
	const getusedFrquList = async () => {
		let res = await getusedFrquSelector({})
		if (res.code == 200) {
			baseFormData.frqusekect = res.data.map(item => {
				return {
					text: item.key,
					value: item.value
				}
			})
		} else {
			uni.showToast({
				duration: 2000,
				title: res.msg,
				icon: "error"
			})
		}
	}
	// 单位
	const getunitFrquList = async () => {
		let res = await getunitFrquSelector({})
		if (res.code == 200) {
			baseFormData.param = res.data.map(item => {
				return {
					text: item.key,
					value: item.value
				}
			})
		} else {
			uni.showToast({
				duration: 2000,
				title: res.msg,
				icon: "error"
			})
		}
	}
	// 药品搜索
	const getdrugList = async () => {
		let res = await getdrugSelector({
			searchKey: data.drugValue
		})
		if (res.code == 200) {
			baseFormData.searchList = res.data
		} else {
			uni.showToast({
				duration: 2000,
				title: res.msg,
				icon: "error"
			})
		}
	}

	// 项目搜索
	const gettherapyList = async () => {
		let res = await gettherapySelector({
			SearchKey: data.projectValue
		})
		if (res.code == 200) {
			baseFormData.therapyList = res.data
		} else {
			uni.showToast({
				duration: 2000,
				title: res.msg,
				icon: "error"
			})
		}
	}
	// 详情
	const getDeatils = async (id, type) => {
		let res = await getDeatilsApi({
			id
		})
		if (res.code == 200) {
			let array = []
			if (type == 0) {
				popupitem.value.open('bottom')
				data.temelapeInfo = res.data.template
			} else {
				popup.value.close()
			}
			data.template = res.data.template
			data.template.PatternName = res.data.patternDescribe
			baseFormData.templateDetailList = res.data.prescriptions
			res.data.prescriptions.forEach(item => {
				array = [...array, ...item.details]
			})
			baseFormData.drugList = array
			console.log(baseFormData.drugList);
		} else {
			uni.showToast({
				duration: 2000,
				title: res.msg,
				icon: "error"
			})
		}
	}
	// 模板列表
	const getTemplateList = async () => {
		let res = await getTemplateListApi({
			Current: 1,
			Pattern: 9,
			SearchKey: "",
			Size: 100,
			total: 0,
		})
		if (res.code == 200) {
			baseFormData.temelapeList = res.data.records
			popup.value.open('bottom')
		} else {
			uni.showToast({
				duration: 2000,
				title: res.msg,
				icon: "error"
			})
		}
	}
	const moreClick = () => {
		if (baseFormData.more == '编辑更多') baseFormData.more = '收缩'
		else baseFormData.more = '编辑更多'
	}
	const drugChange = (row) => {
		let bool = baseFormData.drugList.findIndex(item => item.name == row.name)
		baseFormData.drugList.splice(bool, 1)
	}
	const applyClick = async () => {
		getTemplateList()
	}
	const SaveClick = () => {
		popup1.value.open('bottom')
	}
	const viewClick = (row) => {
		getDeatils(row.templateId, 0)
	}
	const useClick = (row) => {
		getDeatils(row.templateId, 1)

	}
	// 存模板
	const enterAddTemplate = async () => {
		if (!data.template.Name) {
			uni.showToast({
				title: '请填写模板名称',
				duration: 2000,
			})
			return
		}
		if (data.template.IsPersonalTemplate == 0) {
			data.template.IsPersonalTemplate = true
		} else {
			data.template.IsPersonalTemplate = false
		}
		let res = await SetAddTtemplateApi({
			Template: data.template,
			caseHistory: {},
			prescriptions: [{
				itemType: data.template.name,
				details: baseFormData.drugList
			}],
			dises: []
		})
		if (res.code == 200) {
			popup1.value.close()
		} else {
			uni.showToast({
				duration: 2000,
				title: res.msg,
				icon: "error"
			})
		}
	}
	const onClickScan = async () => {
		try {

			let res = await uni.scanCode({
				onlyFromCamera: true, // 仅允许相机扫描
				scanType: ['barCode'], // 指定扫描的码类型为二维码

			})
			parameter.current = 1
			parameter.searchKey = res.result
			await request()
		} catch (e) {
			if (e) {
				uni.showToast({
					title: '扫描失败',
					duration: 2000,
					icon: 'error',
				});
			}
		}
	}
	const drugSearch = (res) => {
		getdrugList()
	}

	const therapySearch = (res) => {
		gettherapyList()
	}
	const addDrug = (row) => {
		if (baseFormData.drugList.some(item => item.id == row.id)) {
			uni.showToast({
				title: '药品/项目已存在',
				duration: 2000,
				icon: 'error',
			});
		} else {
			baseFormData.drugList.push(row)
		}
	}
	const closeDrug = () => {
		data.drugValue = ''
		baseFormData.searchList = []
		data.projectValue = ''
		baseFormData.therapyList = []
	}
	const value1Change = () => {
		if (fromData.value1) {
			fromData.value2 = ''
		}
		if (fromData.value1 > AllMoney.value) {
			fromData.value1 = AllMoney.value
			money.value = 0
		} else {
			money.value = AllMoney.value - fromData.value1
		}
	}
	const value2Change = () => {
		if (fromData.value2) {
			fromData.value1 = ''
		}
		money.value = AllMoney.value * (fromData.value2 / 10)
	}
	const value4Change = () => {
		if (fromData.value4.length > 0) fromData.value3 = AllMoney.value
		else fromData.value3 = ''
	}
	const enterPay = () => {
		popup_user.value.open('bottom')
	}
	const payFnClick = (item) => {
		data.payId = item.id
		if (!fromData.value3) {
			fromData.value3 = AllMoney.value
		}
	}
	const userInfoenter = async () => {
		if (!userFormData.name) {
			uni.showToast({
				title: '请填写患者名称',
				duration: 1500,
				icon: 'error'
			})
			return
		}
		if (!userFormData.sex) {
			uni.showToast({
				title: '请选择患者性别',
				duration: 1500,
				icon: 'error'
			})
			return
		}
		if (!userFormData.age) {
			uni.showToast({
				title: '请填写患者年龄',
				duration: 1500,
				icon: 'error'
			})
			return
		}
		if (!userFormData.tel) {
			uni.showToast({
				title: '请填写患者联系方式',
				duration: 1500,
				icon: 'error'
			})
			return
		}
		if (!userFormData.card) {
			uni.showToast({
				title: '请填写患者身份证号码',
				duration: 1500,
				icon: 'error'
			})
			return
		}
		let res = await settlement(data.settlement)
		if (res.code == 200) {
			uni.showToast({
				title: res.msg,
				duration: 2000,
				icon: 'success'
			})
			popup_pay.value.close()
			popup_user.value.close()
			fromData.value1 = ''
			fromData.value2 = ''
			fromData.value3 = ''
			fromData.value4 = []
			fromData.value5 = ''
		} else {
			uni.showToast({
				title: res.msg,
				duration: 1500,
				icon: 'error'
			})
		}
	}
	onMounted(() => {
		getunitFrquList()
		getusedFrquList()
		if (data.prescriptionId) {
			getDetailApi({
				id: data.prescriptionId
			}).then((res) => {
				if (res.code == 200) {
					data.patient = res.data.patient
					baseFormData.userInfo = {
							name: res.data.patient.name,
							tel: res.data.patient.tel,
							gender: res.data.patient.gender,
							birthday: res.data.patient.birthday,
							age: res.data.patient.age,
							certno: res.data.patient.certno,
						},
						baseFormData.drugList = res.data.prescriptionDetails
				} else {
					uni.showToast({
						duration: 2000,
						title: res.msg,
						icon: "error"
					})
				}
			})
		}
	})
</script>

<style lang="scss" scoped>
	.retail {
		width: 100%;
		height: calc(100vh - 100rpx);
		overflow: auto;
		background-color: #fafafa;

		.from_user_info {
			width: 100%;

			::v-deep .uni-forms {
				.uni-forms-item {
					height: 80rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					background-color: #fff;
					margin-bottom: 2rpx;

					.uni-forms-item__label {
						width: auto !important;
						font-size: 24rpx;
					}

					.uni-forms-item__content {
						height: 100%;
						flex: unset;
						display: flex;
						align-items: center;

						.uni-easyinput {
							margin-right: 15rpx;
							flex: unset;
							width: 200rpx;

							.is-input-border {
								border: unset;
							}
						}

						.more {
							flex: 1;
							padding: 10rpx 20rpx;
							box-sizing: border-box;
							font-size: 24rpx;
							background-color: #f1f1f1;
							line-height: 80rpx;
							color: #4d8ff0;
						}

						.checklist-box {
							margin-right: 20rpx;

							.checklist-content {
								.checklist-text {
									font-size: 24rpx;
								}
							}

							&:last-child {
								margin-right: 15rpx;
							}
						}

						picker {
							display: flex;
							align-items: center;
							margin-right: 15rpx;
						}
					}
				}
			}
		}

		.Template_btn {
			width: 100%;
			height: 100rpx;
			display: flex;
			align-items: center;

			view {
				border: 1rpx #409eff solid;
				border-radius: 5rpx;
				padding: 10rpx 20rpx;
				box-sizing: border-box;
				font-size: 24rpx;
				margin-left: auto;
				color: #409eff;
			}

			.apply {
				margin: 0rpx 20rpx;
			}
		}

		.add_drug {
			padding: 20rpx 20rpx 0rpx;
			box-sizing: border-box;
			background-color: #fff;
			position: relative;

			.drug_title {
				color: #409eff;
			}

			.search_drug {
				margin-top: 20rpx;
				display: flex;
				align-items: center;
				padding-bottom: 20rpx;
				border-bottom: 1px #dfdfdf solid;

				image {
					width: 80rpx;
					height: 80rpx;
				}
			}

			.search {
				position: fixed;
				top: 0;
				left: 0;
				width: 100vw;
				height: 100vh;
				z-index: 99;

				.search_result {
					width: 630rpx;
					max-height: 40vh;
					overflow-y: auto;
					padding: 20rpx 0rpx;
					font-size: 26rpx;
					position: absolute;
					background-color: #fff;
					top: 425rpx;
					left: 20rpx;
					border: 1rpx solid #f1f1f1;

					.search_item {
						.drug_text {
							.drug_text_title {
								.dispensing {
									font-size: 22rpx;
									padding: 6rpx;
									border-radius: 6rpx;
									color: #FFFFFF;
								}

								.green {
									background-color: limegreen;
								}

								.red {

									background-color: red;
								}

								.name {
									font-weight: 700;
									padding: 0rpx 15rpx;
								}

								.lable {
									color: #888888;
									padding: 0rpx 8rpx;
								}

								.money {
									color: red;
								}
							}

							.drug_text_desc {
								margin-top: 8rpx;
								color: #888888;

								text:last-child {
									margin-left: 10rpx;
								}
							}
						}
					}
				}
			}
		}

		.add_project {
			margin-top: 20rpx;
			padding: 20rpx 20rpx 0rpx;
			box-sizing: border-box;
			background-color: #fff;

			.project_title {
				color: #409eff;
			}

			.search_project {
				margin-top: 20rpx;
				padding-bottom: 20rpx;
				border-bottom: 1rpx #dfdfdf solid;
			}

			.search {
				position: fixed;
				top: 0;
				left: 0;
				width: 100vw;
				height: 100vh;
				z-index: 99;

				.search_result {
					width: 710rpx;
					max-height: 40vh;
					overflow-y: auto;
					padding: 20rpx 0rpx;
					font-size: 26rpx;
					position: absolute;
					background-color: #fff;
					top: 625rpx;
					left: 20rpx;
					border: 1rpx solid #f1f1f1;

					.search_item {
						.drug_text {
							display: flex;
							align-items: center;
							justify-content: space-between;

							.drug_text_title {
								.dispensing {
									font-size: 22rpx;
									padding: 6rpx;
									border-radius: 6rpx;
									color: #FFFFFF;
								}

								.green {
									background-color: limegreen;
								}

								.red {

									background-color: red;
								}

								.name {
									font-weight: 700;
									padding: 0rpx 15rpx;
								}

								.lable {
									color: #888888;
									padding: 0rpx 8rpx;
								}

								.money {
									color: red;
								}
							}

							.drug_text_desc {
								margin-top: 8rpx;
								color: #888888;

								text:last-child {
									margin-left: 10rpx;
								}
							}
						}
					}
				}
			}
		}

		.view_ul {
			width: 100%;

			.view_li {
				width: 100%;
				padding: 10rpx 0rpx 20rpx;
				box-sizing: border-box;

				.li_title {
					display: flex;
					align-items: center;
					justify-content: space-around;

					.name {
						width: 200rpx;
						font-weight: bold;
					}

					.money {
						display: flex;
						align-items: center;

						::v-deep .uni-easyinput {
							margin-right: 15rpx;
							flex: unset;
							width: 100rpx;
							border-bottom: 1rpx #dfdfdf solid;

							.is-input-border {
								border: unset;
							}
						}

						text {
							font-size: 26rpx;

							&:last-child {
								color: red;
							}
						}
					}

					.number {
						display: flex;
						align-items: center;
						font-size: 26rpx;

						::v-deep .uni-numbox__value {
							width: 65rpx;
						}

						::v-deep .uni-numbox-btns {
							padding: 0 14rpx;
						}
					}

					.detele {

						image {
							width: 65rpx;
							height: 65rpx;
						}
					}
				}

				.li_desc {
					font-size: 24rpx;
					color: #dfdfdf;

					text {
						margin-right: 8 rpx;
					}
				}

				.li_params {
					display: flex;
					align-items: center;
					font-size: 26rpx;

					.params {
						display: flex;
						align-items: center;

						.value {
							width: 160rpx;
							border-bottom: 1rpx #dfdfdf solid;
							margin-right: 15rpx;
							line-height: 70rpx;
							padding-left: 10rpx;

						}

						::v-deep .uni-easyinput {
							margin-right: 15rpx;
							flex: unset;
							width: 100rpx;
							border-bottom: 1rpx #dfdfdf solid;

							.is-input-border {
								border: unset;
							}
						}
					}

					.use {
						margin-left: 20rpx;
						display: flex;
						align-items: center;

						.value {
							width: 160rpx;
							border-bottom: 1rpx #dfdfdf solid;
							margin-right: 15rpx;
							line-height: 70rpx;
							padding-left: 10rpx;

						}
					}
				}
			}
		}

		.footers {
			width: 100%;
			height: 100rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			position: fixed;
			left: 0;
			bottom: 0;
			background-color: #fff;

			.money {
				color: red;
			}

			.operation {
				display: flex;
				align-items: center;

				.registration,
				.charge {
					color: #fff;
					line-height: 100rpx;
					border-radius: 8rpx;
					padding: 0rpx 70rpx;
				}

				.registration {
					background-color: sandybrown;
				}

				.charge {
					background-color: #409eff;
				}
			}
		}

		::v-deep .withdrawal {

			.popup-content {
				min-height: 80vh;
				overflow-y: auto;
				background-color: #fff;
				border-radius: 15rpx 15rpx 0rpx 0rpx;

				.popup_title {
					font-size: 35rpx;
					font-weight: 700;
					display: flex;
					height: 100rpx;
					align-items: center;
					justify-content: center;
					border-radius: 15rpx 15rpx 0rpx 0rpx;
					position: relative;
					border-bottom: 1rpx solid #dfdfdf;

					image {
						width: 40rpx;
						height: 40rpx;
						position: absolute;
						top: 10rpx;
						right: 15rpx;
					}
				}
			}

			.drug_box {
				height: 90rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 0rpx 20rpx;
				box-sizing: border-box;
				border-bottom: 1rpx solid #f1f1f1;

				.popup_btn {
					display: flex;
					align-items: center;

					&>view {
						padding: 10rpx 20rpx;
						box-sizing: border-box;
						border: 1rpx #409eff solid;
					}

					.close {
						color: #409eff;
						background-color: #fff;
					}

					.enter {
						color: #fff;
						background-color: #409eff;
						margin-left: 15rpx;
					}
				}
			}
		}

		::v-deep .withdrawal1 {

			.popup-content {
				background-color: #fff;
				border-radius: 15rpx 15rpx 0rpx 0rpx;

				.popup_title {
					font-size: 35rpx;
					font-weight: 700;
					display: flex;
					height: 100rpx;
					align-items: center;
					justify-content: center;
					border-radius: 15rpx 15rpx 0rpx 0rpx;
					position: relative;
					border-bottom: 1rpx solid #dfdfdf;

					image {
						width: 40rpx;
						height: 40rpx;
						position: absolute;
						top: 10rpx;
						right: 15rpx;
					}
				}

				.popupitem {
					max-height: 80vh;
					overflow-y: auto;

					.popupitem_ul {
						width: 100%;
						padding: 0rpx 20rpx 20rpx;
						box-sizing: border-box;

						.popupitem_li {
							font-size: 26rpx;
							line-height: 70rpx;
							border-bottom: 1rpx solid #f1f1f1;

							text {
								display: inline-block;
								color: #dfdfdf;

								&:first-child {
									color: #000;
									min-width: 100rpx;
									text-align: right;
								}
							}
						}
					}

					.drug_ul,
					.project_ul {
						width: 100%;
						margin-bottom: 10rpx;

						.drug_title,
						.project_title {
							line-height: 80rpx;
							border-top: 1rpx solid #dfdfdf;
							border-bottom: 1rpx solid #dfdfdf;
						}

						.drug_li,
						.project_li {
							padding: 20rpx;
							box-sizing: border-box;
							border-bottom: 1rpx #f1f1f1 solid;

							.top {
								display: flex;
								align-items: center;
								justify-content: space-between;
								padding-bottom: 20rpx;

								.text_title {
									font-weight: 700;
								}

								.param {
									display: flex;

									&>view {
										font-size: 26rpx;

										&:last-child {
											margin-left: 10rpx;
										}

										text {
											color: red;
										}
									}
								}
							}

							.bottom {
								width: 100%;
								display: flex;
								flex-wrap: wrap;

								&>view {
									font-size: 26rpx;
									color: #dfdfdf;
									min-width: 33%;
								}
							}
						}
					}

				}
			}
		}

		::v-deep .withdrawal2 {
			.popup-content {
				height: 80vh;
				background-color: #fff;
				border-radius: 15rpx 15rpx 0rpx 0rpx;
				position: relative;

				.popup_title {
					font-size: 35rpx;
					font-weight: 700;
					display: flex;
					height: 100rpx;
					align-items: center;
					justify-content: center;
					border-radius: 15rpx 15rpx 0rpx 0rpx;
					position: relative;
					border-bottom: 1rpx solid #dfdfdf;

					image {
						width: 40rpx;
						height: 40rpx;
						position: absolute;
						top: 10rpx;
						right: 15rpx;
					}
				}

				.add_temelate {
					min-height: 80vh;
					overflow-y: auto;

					.uni-forms {
						width: 100%;

						.uni-forms-item {
							width: 100%;
							padding-left: 15rpx;
							box-sizing: border-box;
							margin-bottom: 0rpx;
							border-bottom: 1rpx #f1f1f1 solid;
							display: flex;
							align-items: center;

							.uni-forms-item__label {
								width: 150rpx !important;
							}

							.uni-forms-item__content {
								.uni-easyinput {
									margin-right: 15rpx;
									flex: unset;

									.is-input-border {
										border: unset;
									}
								}
							}

							&:last-child {

								.uni-forms-item__label {
									text {
										display: none;
									}
								}
							}
						}
					}
				}

				.popup_btn {
					width: 100%;
					position: absolute;
					bottom: 0rpx;
					left: 0rpx;
					background-color: #FFFFFF;
					display: flex;
					align-items: center;
					justify-content: center;
					border-top: 1rpx solid #f1f1f1;
					padding: 15rpx 0rpx;

					view {
						width: 45%;
						font-size: 28rpx;
						line-height: 70rpx;
						text-align: center;
						border: 1rpx solid #409eff;
					}

					.close {
						color: #409eff;
					}

					.enter {
						color: #fff;
						background-color: #409eff;
						margin-left: 20rpx;
					}
				}
			}
		}

		::v-deep .pay {
			.popup-content {
				min-height: 50vh;
				background-color: #fff;
				border-radius: 15rpx 15rpx 0rpx 0rpx;
				position: relative;
				padding-bottom: 110rpx;

				.popup_title {
					font-size: 28rpx;
					display: flex;
					height: 80rpx;
					align-items: center;
					justify-content: center;
					border-radius: 15rpx 15rpx 0rpx 0rpx;
					position: relative;
					border-bottom: 1rpx solid #dfdfdf;
				}

				.Refund {
					padding: 0rpx 20rpx;
					box-sizing: border-box;

					.function {
						font-size: 26rpx;
						line-height: 75rpx;
						border-bottom: 1rpx #dfdfdf solid;

						text {
							margin-right: 10rpx;
						}
					}

					.desc {
						font-size: 26rpx;
						padding: 20rpx 0rpx;
						box-sizing: border-box;
						display: flex;

						text {
							margin-right: 10rpx;
						}
					}
				}

				.repair {
					padding: 0rpx 20rpx;
					box-sizing: border-box;

					.repair_ul {

						.repair_title,
						.repair_li {
							display: flex;
							align-items: center;
							font-size: 26rpx;
							line-height: 75rpx;
							border-bottom: 1rpx #dfdfdf solid;

						}

						.repair_title {
							color: #409eff;
						}

						.repair_li {
							.red {
								font-size: 28rpx;
								color: red;
							}

							&:nth-child(3) {
								.uni-easyinput {
									margin-right: 15rpx;
									flex: unset;
									width: 100rpx;

									.uni-easyinput__content-input {
										height: 50rpx;
									}


								}

								.text_margin {
									padding: 0rpx 15rpx;
								}

								// picker {
								// 	width: 100rpx;
								// 	line-height: 70rpx;
								// 	border-bottom: 1rpx #dfdfdf solid;
								// }
							}

							&:nth-child(5) {
								.uni-easyinput {
									margin: 0rpx 15rpx;
									flex: unset;
									width: 150rpx;

									.uni-easyinput__content-input {
										height: 50rpx;
									}
								}

								label {
									margin-left: 30rpx;

									checkbox {
										transform: scale(0.8);
									}
								}
							}

							&:last-child {
								.uni-easyinput {
									margin: 0rpx 15rpx;

									.uni-easyinput__content-input {
										height: 50rpx;
									}
								}

								border-bottom: none
							}
						}

					}
				}

				.Method_title {
					line-height: 60rpx;
					font-size: 26rpx;
					padding-left: 20rpx;
					background-color: #dfdfdf;
				}

				.RefundMethod {
					.Method_title {
						color: #409eff;
						background-color: #fff;
					}

					.pay_ul {
						padding: 0rpx 20rpx;
						box-sizing: border-box;
						display: flex;
						flex-wrap: wrap;
						justify-content: space-between;

						.pay_li {
							width: 28%;
							border: 1rpx #dfdfdf solid;
							border-radius: 10rpx;
							line-height: 60rpx;
							font-size: 26rpx;
							text-align: center;
							margin-top: 15rpx;
						}

						.active {
							border: 1rpx #409eff solid;
						}
					}
				}

				.popup_btn {
					width: 100%;
					position: absolute;
					bottom: 0rpx;
					left: 0rpx;
					background-color: #FFFFFF;
					display: flex;
					align-items: center;
					justify-content: center;
					border-top: 1rpx solid #f1f1f1;
					padding: 15rpx 0rpx;

					view {
						width: 45%;
						font-size: 28rpx;
						line-height: 70rpx;
						text-align: center;
						border: 1rpx solid #409eff;
					}

					.close {
						color: #409eff;
					}

					.enter {
						color: #fff;
						background-color: #409eff;
						margin-left: 20rpx;
					}
				}
			}
		}

		::v-deep .uni-easyinput {
			.uni-easyinput__content-input {
				font-size: 26rpx;
			}

			.content-clear-icon {
				display: none;
			}
		}

		::v-deep .add_user_Info {
			.popup-content {
				min-height: 80vh;
				background-color: #fff;
				border-radius: 15rpx 15rpx 0rpx 0rpx;
				position: relative;
				padding-bottom: 110rpx;

				.popup_title {
					font-size: 28rpx;
					display: flex;
					height: 80rpx;
					align-items: center;
					justify-content: center;
					border-radius: 15rpx 15rpx 0rpx 0rpx;
					position: relative;
					border-bottom: 1rpx solid #dfdfdf;
				}

				.uni-forms {
					.uni-forms-item {
						margin-bottom: 0rpx;
						border-bottom: 1rpx solid #dfdfdf;

						.uni-forms-item__label {
							width: 180rpx !important;
						}

						.uni-forms-item__content {
							display: flex;
							align-items: center;

							.uni-easyinput {
								.uni-easyinput__content {
									border: unset;

									.uni-easyinput__content-input {
										padding-left: 0 !important;
									}
								}
							}
						}
					}
				}

				.popup_btn {
					width: 100%;
					position: absolute;
					bottom: 0rpx;
					left: 0rpx;
					background-color: #FFFFFF;
					display: flex;
					align-items: center;
					justify-content: center;
					border-top: 1rpx solid #f1f1f1;
					padding: 15rpx 0rpx;

					view {
						width: 45%;
						font-size: 28rpx;
						line-height: 70rpx;
						text-align: center;
						border: 1rpx solid #409eff;
					}

					.close {
						color: #409eff;
					}

					.enter {
						color: #fff;
						background-color: #409eff;
						margin-left: 20rpx;
					}
				}
			}
		}

		::v-deep .withdrawal3 {
			.popup-content {
				height: 40vh;
				background-color: #fff;
				border-radius: 15rpx 15rpx 0rpx 0rpx;
				position: relative;

				.picker-view {
					height: 100%;
					margin-top: 20rpx;
				}

				.item {
					line-height: 100rpx;
					text-align: center;
					font-size: 26rpx;
				}

				.popup_btn {
					width: 100%;
					position: absolute;
					bottom: 0rpx;
					left: 0rpx;
					background-color: #FFFFFF;
					display: flex;
					align-items: center;
					justify-content: center;
					border-top: 1rpx solid #f1f1f1;
					padding: 15rpx 0rpx;

					view {
						width: 45%;
						font-size: 28rpx;
						line-height: 70rpx;
						text-align: center;
						border: 1rpx solid #409eff;
					}

					.close {
						color: #409eff;
					}

					.enter {
						color: #fff;
						background-color: #409eff;
						margin-left: 20rpx;
					}
				}
			}
		}
	}
</style>